package gov.fnal.xpath2ldap.util;

import javax.xml.transform.TransformerFactory;
import javax.xml.transform.TransformerException;
import javax.xml.transform.Transformer;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.dom.DOMResult;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.parsers.DocumentBuilder;
import java.net.URL;
import java.io.IOException;

import org.w3c.dom.Document;
import org.xml.sax.SAXException;


public class PrettyTransform {

    private static TransformerFactory tFactory;
    private static Transformer prettyXform;

    public static Transformer getPrettyXform() {

        if(prettyXform==null) {
            tFactory = TransformerFactory.newInstance();

            if(tFactory.getFeature(DOMSource.FEATURE) && tFactory.getFeature(DOMResult.FEATURE))
            {
                try {
                    //Instantiate a DocumentBuilderFactory.
                    System.setProperty("javax.xml.parsers.XmlDocumentParserFactory", "org.apache.xerces.jaxp.DocumentBuilderFactoryImpl");
                    DocumentBuilderFactory dFactory = DocumentBuilderFactory.newInstance();

                    // And setNamespaceAware, which is required when parsing xsl files
                    dFactory.setNamespaceAware(true);

                    //Use the DocumentBuilderFactory to create a DocumentBuilder.
                    DocumentBuilder dBuilder = dFactory.newDocumentBuilder();

                    //Use the DocumentBuilder to parse the XSL stylesheet.
                    URL prettyXsl = DnSortTransform.class.getResource("/pretty.xsl");
                    Document dnSortDoc = dBuilder.parse(prettyXsl.openStream());

                    // Use the DOM Document to define a DOMSource object.
                    DOMSource xslDomSource = new DOMSource(dnSortDoc);

                    // Set the systemId: note this is actually a URL, not a local filename
                    xslDomSource.setSystemId("pretty_.xsl");

                    // Process the stylesheet DOMSource and generate a Transformer.
                    prettyXform = tFactory.newTransformer(xslDomSource);

                } catch (ParserConfigurationException pce) {
                    System.err.println("ParserConfigurationException");
                } catch (IOException pce) {
                    System.err.println("IOException");
                } catch (TransformerException pce) {
                    System.err.println("TransformerException");
                } catch (SAXException pce) {
                    System.err.println("SAXException");
                }
            }  else
            {
                System.err.println("DOM node processing not supported!");
            }
        }

        return prettyXform;
    }


    public static Document doPrettyTransform(Document doc_in) {
        // Redo whitespace of document
        DOMSource xmlDomSource = new DOMSource(doc_in);
        xmlDomSource.setSystemId("InDoc");
        DOMResult domResult = new DOMResult();
        try {
            getPrettyXform().transform(xmlDomSource, domResult);
        } catch (
                TransformerException pce) {
            System.err.println("TransformerException");
        }

        return (Document) domResult.getNode();
    }
}